﻿using System;
using System.Collections.Generic;
using System.Data.OracleClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Model;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Infrastructure.Common;
using System.Data;
using System.Text;

namespace Xiaov.SmartWeb.Application.PluginSrc.StatisticalSystem.Manage
{
  public partial class Index : BasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      PM_Filter();
      AjaxPro.Utility.RegisterTypeForAjax(typeof(Index));
      if (!IsPostBack)
      {
        BindDDL();
        BindGrid();
      }
    }

    private void BindGrid()
    {
      if (!Maticsoft.DBUtility.DbHelperOra.Exists("select count(1) from bbtbqk where dwbm='" + ddl_Owner.SelectedValue + "' and NF=" + ddl_Year.SelectedValue + " and YF=" + ddl_Month.SelectedValue))
      {
        LoadProc("p_bbtbqk", ddl_Owner.SelectedValue, "NULL", ddl_Year.SelectedValue, ddl_Month.SelectedValue.ToInt32().ToString());
      }
    }

    [AjaxPro.AjaxMethod]
    public void Check(string bbbm, string dwbm, string year, string month)
    {
      //数据校验
      LoadProc("p_check", dwbm, "NULL", year, month);
    }

    //[AjaxPro.AjaxMethod]
    //public void Adjust(string bbbms,string dwbm,string year,string months,string sbr)
    //{           
    //    string[] bb = bbbms.Split(',');
    //    string[] mons = months.Split(',');

    //    StringBuilder sb = new StringBuilder();
    //    sb.Append("begin ");
    //    for (int i = 0; i < bb.Length; i++)
    //    {
    //        sb.Append("update bbtbqk set sfsh='是' , SHR='" + sbr + "' , SHSJ= to_date('" + DateTime.Now + "','yyyy-mm-dd hh24:mi:ss') where BBBM='" + bb[i] + "' and DWBM='" 
    //            + dwbm + "' and NF=" + year + " and YF =" + mons[i] + ";");
    //    }
    //    sb.Append("null;end;");
    //    Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sb.ToString());
    //}

    [AjaxPro.AjaxMethod]
    public void UnLock(string bbbms, string dwbm, string year, string months)
    {
      string[] bb = bbbms.Split(',');
      string[] mons = months.Split(',');
      StringBuilder sb = new StringBuilder();
      sb.Append("begin ");
      for (int i = 0; i < bb.Length; i++)
      {
        sb.Append("update bbtbqk set bbzt='未上报' , TJSJ=null where BBBM='" + bb[i] + "' and DWBM='" + dwbm + "' and NF=" + year + " and YF=" + mons[i] + ";");
      }
      sb.Append("null;end;");
      Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sb.ToString());
    }

    [AjaxPro.AjaxMethod]
    public void Compute(string bbbms, string dwbm, string year, string months)
    {
      string[] bb = bbbms.Split(',');
      string[] mons = months.Split(',');

      for (int i = 0; i < bb.Length; i++)
      {
        LoadProc("p_compute", dwbm, bb[i], year, mons[i]);
      }
    }

    [AjaxPro.AjaxMethod]
    public void Report(string bbbms, string dwbm, string year, string months, string sbr)
    {
      string[] bb = bbbms.Split(',');
      string[] mons = months.Split(',');
      StringBuilder sb = new StringBuilder();
      sb.Append("begin ");
      for (int i = 0; i < bb.Length; i++)
      {
        sb.Append("update bbtbqk set bbzt='已上报' , TJR='" + sbr + "' , TJSJ= to_date('" + DateTime.Now + "','yyyy-mm-dd hh24:mi:ss') where BBBM='" + bb[i] + "' and DWBM='" + dwbm + "' and NF=" + year + " and YF=" + mons[i] + ";");
      }
      sb.Append("null;end;");
      Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sb.ToString());
    }

    #region 绑定下拉菜单及调用过程

    private void BindDDL()
    {
      System.Data.DataSet ds = Maticsoft.DBUtility.DbHelperOra.Query("select distinct(t.DWBM),y.dwmc from bbdytbdw t,dwbmb y where t.dwbm=y.dwbm and length(t.dwbm)>=2 order by t.dwbm asc");
      System.Data.DataView dv = ds.Tables[0].DefaultView;
      dv.RowFilter = "dwbm like '" + PUser.USERDEPT + "%'";
      ddl_Owner.DataSource = dv;
      ddl_Owner.DataTextField = "DWMC";
      ddl_Owner.DataValueField = "DWBM";
      ddl_Owner.DataBind();

      //ddl_Owner.Items.Remove("HB");
      string dwParent = Maticsoft.DBUtility.DbHelperOra.GetSingle("select f_getparentdw('"+PUser.USERDEPT+"') from dual").ToString();
      if (dwParent != PUser.USERDEPT)
      {
        btnLock.Visible = false;
      }

      int thisyear = DateTime.Now.Year;
      int thismonth = DateTime.Now.Month;
      int preyear = 2006;
      for (int year = preyear; year <= thisyear + 1; year++)
      {
        ddl_Year.Items.Add(year.ToString());
      }
      for (int i = 1; i <= 12; i++)
      {
        ddl_Month.Items.Add(i.ToString());
      }
      ddl_Year.SelectedValue = thisyear.ToString();
      ddl_Month.SelectedValue = (thismonth - 1).ToString();


      string sql = "select UserName from s_pm_user where loginname='" + PUser.LOGINNAME + "'";
      ddl_Sbr.DataSource = Maticsoft.DBUtility.DbHelperOra.Query(sql);
      ddl_Sbr.DataTextField = "UserName";
      ddl_Sbr.DataValueField = "UserName";
      ddl_Sbr.DataBind();

    }

    protected void ddl_Owner_SelectedIndexChanged(object sender, EventArgs e)
    {
      BindGrid();
    }

    protected void ddl_Year_SelectedIndexChanged(object sender, EventArgs e)
    {
      BindGrid();
    }


    protected void ddl_Month_SelectedIndexChanged(object sender, EventArgs e)
    {
      BindGrid();
    }

    /// <summary>
    /// 调用存储过程
    /// </summary>
    /// <param name="ProcdureName">过程名称</param>
    /// <returns>返回值</returns>
    public bool LoadProc(string ProcdureName, string dwbm, string bbbm, string year, string month)
    {
      OracleParameter[] parameters ={
            new OracleParameter("v_bbbm",OracleType.VarChar,20),
　　     new OracleParameter("v_dwbm",OracleType.VarChar,20),
　　     new OracleParameter("v_nf",OracleType.Number,4),
            new OracleParameter("v_yf",OracleType.Number,2)
　　      };
      parameters[0].Value = bbbm;
      parameters[1].Value = dwbm;
      parameters[2].Value = year;
      parameters[3].Value = month;
      try
      {
        OracleDataReader odr = Maticsoft.DBUtility.DbHelperOra.RunProcedure(ProcdureName, parameters);
        odr.Close();
        return true;
      }
      catch
      {
        return false;
      }
    }

    #endregion

    protected void btnLock_Click(object sender, EventArgs e)
    {
      OracleParameter[] parameters ={
　　     new OracleParameter("v_nf",OracleType.Number,4),
            new OracleParameter("v_yf",OracleType.Number,2),
　　      };
      parameters[0].Value = ddl_Year.SelectedValue;
      parameters[1].Value = ddl_Month.SelectedValue;
      try
      {
        OracleDataReader odr = Maticsoft.DBUtility.DbHelperOra.RunProcedure("p_bbtbqk_all", parameters);
        odr.Close();
      }
      catch
      {

      }

    }
  }
}